﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common
{
	[XmlRoot("Site", IsNullable = false, Namespace = "")]
    public class Site
    {
        public SiteNames SiteNames;
        public List<string> Domains;
		public List<RuleSet> RuleSets;
		public RuleSet this[string ruleName]
		{
			get {
				foreach (RuleSet rule in RuleSets)
				{ if (rule.Name == ruleName) return rule; }
				return null;
			}
			set
			{
				foreach (RuleSet rule in RuleSets)
				{ if (rule.Name == ruleName) { RuleSets.Remove(rule); break; } }
				value.Name = ruleName;
				RuleSets.Add(value);
			}
		}
		
        private void Init(string[] domains, string encoding)
        {
			RuleSets = new List<RuleSet>();
            Domains = new List<string>(domains);
         
           
			SiteNames = new SiteNames();
        }
        private void Init(string domain, string encoding)
        {
            Init(new string[] { domain }, encoding);
        }
        public Site(string domain)
        {
            Init(domain, Encoding.UTF8.HeaderName);
        }
        public Site(string domain, string encoding)
        {
            Init(domain, encoding);
        }
        public Site(string[] domains)
		{ Init(domains, Encoding.UTF8.HeaderName); }
        public Site(string[] domains, string encoding)
        {
            Init(domains, encoding);
        }
		public Site()
		{
			Init("", Encoding.UTF8.HeaderName);
		}


        public bool InDomain(Uri uri) 
        { 
            string uriDomain = uri.Host;

            foreach (string s in Domains)
            {
                //First domain from XML file is "", ignore it.
                if (uriDomain.Contains(s) && s != string.Empty) return true;
            }
            return false;
        }
		public override string ToString()
		{
			foreach (string domain in Domains) if (domain != string.Empty) return domain;
			return string.Empty;
		}

    }

    [Serializable]
    public class SiteNames
    {
        public string SiteName;

        public string SiteNameProperty
        {
            get { return SiteName; }
            set { SiteName = value; }
        }
        public string DisplayName;

        public string DisplayNameProperty
        {
            get { return DisplayName; }
            set { DisplayName = value; }
        }
        public string StoryName;
        public string BlogName;
        public string ForumName;
        public string TalkbackName;

        public SiteNames()
        {
            StoryName = string.Empty;
            BlogName = string.Empty;
            ForumName = string.Empty;
            TalkbackName = string.Empty;
            SiteName = string.Empty;
            DisplayName = string.Empty;
        }
    }
   


	
}
